var core = require('core');
var db = core.db;
var send = core.send;

var list = function(req, res, next){
    var body = req.body;
    var count = parseInt(body.count) || 10;
    var page = parseInt(body.page) || 1;
    var start = count * (page - 1);
    var sort = body.sort && body.sort != -1 ? 1 : -1;
    delete body.count;
    delete body.page;

    db.schema.Merchant.find(body, "-_id -__v -recommend -personnel").skip(start).limit(count).sort({time: sort}).exec(function(err, doc){
        var SEND;
        if(err){
            console.error(err);
            SEND = new send(100000);
        }else{
            SEND = new send(0);
            SEND.docs = doc;
        };
        res.send(SEND);
    });
};

module.exports = list;
